package sunflower.config.mybatis;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;


/**
 * @author: TanJingyu
 * @create: 2021-09-30 16:37
 *
 * 可以拦截的类型为以下四种：
 * Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
 * ParameterHandler (getParameterObject, setParameters)
 * ResultSetHandler (handleResultSets, handleOutputParameters)
 * StatementHandler (prepare, parameterize, batch, update, query)
 *
 * 上边的四种类型都可以写在下边的方法签名中
 * type为类型，method是此类型中的某个方法，args是此方法的入参类型
 */
@Intercepts({@Signature(
        type= Executor.class,
        method = "query",
        args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class})})
public class UserInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
//        for (Object arg : invocation.getArgs()) {
//            if (arg instanceof MappedStatement) {
//                SqlSource sqlSource = ((MappedStatement) arg).getSqlSource();
//                BoundSql boundSql = sqlSource.getBoundSql(null);
//                boundSql.setAdditionalParameter("username", "张飞");
//                System.out.println(sqlSource);
//            }
//        }
        return invocation.proceed();
    }
}
